/******************************************************************************************************
Copyright (C) 2019 Mestrelab Research S.L. All rights reserved.

This script reads reference spectra and looks for these in the experimental scout protons then uses the
spectra from blank, sample with protein and sample with protein and inhibitor spectra to establish activity.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.


*****************************************************************************************************/
/*globals MessageBox, print, File, Dir, mainWindow, TextStream, Zip, FileInfo*/
/*jslint plusplus: true, indent: 4, continue: true*/


function NMReDATACommon() {
	"use strict";
}


NMReDATACommon.addFilesToZip = function (aFiles, aZipFile, aRootPath) {
	"use strict";

	var rawDir, zip, f, i, file, zipPath, fileName, aAppend, fileInfo, dirContents, oneUpDir, rootPath;

	if (typeof (aFiles) === "string") {
		aFiles = [aFiles];
	}
	rootPath = aRootPath;

	aAppend = File.exists(aZipFile) ? true : false;
	zip = new Zip(aZipFile);

	if (zip.open(aAppend) === 0) {
		for (f = 0; f < aFiles.length; f++) {
			fileInfo = new FileInfo(aFiles[f]);

			if (fileInfo.isFile) {
				file = new File(aFiles[f]);
				fileName = file.name.replace(file.absDirPath + "/", "");
				zip.addFile(file.name, fileName, Zip.Default);
			} else if (fileInfo.isDir) {
				rawDir = new Dir(aFiles[f]);
				dirContents = this.getDirContents(rawDir, []);

				if (!rootPath) {
					oneUpDir = new Dir(rawDir.absPath);
					oneUpDir.cdUp();
					rootPath = oneUpDir.absPath;
				}

				for (i = 0; i < dirContents.length; i++) {
					zipPath = dirContents[i].replace(rootPath, "");
					zip.addFile(dirContents[i], zipPath, Zip.Default);
				}
			}
		}
		zip.close();
	}
};


NMReDATACommon.getDirContents = function (aDirPath, aContents) {
	"use strict";

	var i, dir, dirs,
		contents = aContents || [];

	dir = new Dir(aDirPath);
	dirs = dir.entryInfoList("*", Dir.All | Dir.NoDotAndDotDot, Dir.Time);
	for (i = 0; i < dirs.length; i++) {
		if (dirs[i].isDir) {
			contents = this.getDirContents(dirs[i].toString(), contents);
		} else {
			contents.push(dirs[i].absoluteFilePath);
		}
	}
	return contents;
};


NMReDATACommon.compareObjects = function (obj1, obj2) {
	"use strict";

	var p;

	for (p in obj1) {
		if (obj1.hasOwnProperty(p)) {
			if (obj2.hasOwnProperty(p)) {
				switch (typeof (obj1[p])) {
				case 'object':
					if (!this.compareObjects(obj1[p], obj2[p])) {
						return false;
					}
					break;
				case 'function':
					if (obj2[p] === undefined || obj1[p].toString() !== obj2[p].toString()) {
						return false;
					}
					break;
				default:
					if (obj1[p] !== obj2[p]) {
						return false;
					}
				}
			} else {
				return false;
			}
		}
	}

	for (p in obj2) {
		if (obj2.hasOwnProperty(p)) {
			if (obj1[p] === undefined) {
				return false;
			}
		}
	}
	return true;
};


NMReDATACommon.splittingToSegments = function (aSplittingCode) {
	"use strict";

	var splitToSegments = {};

	splitToSegments.s = 0;	//singulet
	splitToSegments.d = 1;	//doublet
	splitToSegments.t = 2;	//triplet
	splitToSegments.q = 3;	//quartet
	splitToSegments.p = 4;	//pentaplet
	splitToSegments.h = 5;	//heptaplet

	return splitToSegments[aSplittingCode] || 0;
};

